package com.example.controller;

import com.example.common.Result;
import com.example.entity.*;
import com.example.service.*;
import com.example.utils.TokenUtils;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/statistics")
public class StatisticsController {

    @Resource
    private CategoryService categoryService;
    @Resource
    private SingService singService;
    @Resource
    private AlbumService albumService;
    @Resource
    private SingerService singerService;
    @Resource
    private PlaylistService playlistService;

    /**
     * 不同分类下歌曲占比统计
     */
    @GetMapping("/pie")
    public Result pie() {
        List<Map<String, Object>> list = new ArrayList<>();
        List<Category> categories = categoryService.selectAll(null);
        List<Sing> sings = singService.selectAll(null);
        for (Category category : categories) {
            Map<String, Object> map = new HashMap<>();
            map.put("name", category.getName());
            map.put("value", sings.stream().filter(x -> x.getCategoryId().equals(category.getId())).count());
            list.add(map);
        }
        return Result.success(list);
    }

    /**
     * Top10 歌单统计
     */
    @GetMapping("/bar1")
    public Result bar1() {
        Map<String, Object> map = new HashMap<>();
        List<String> xList = new ArrayList<>();
        List<Object> yList = new ArrayList<>();

        List<Playlist> playlists = playlistService.selectTop(10);
        for (Playlist playlist : playlists) {
            xList.add(playlist.getName());
            yList.add(playlist.getHot());
        }
        map.put("xList", xList);
        map.put("yList", yList);
        return Result.success(map);
    }

    /**
     * Top10 歌手统计
     */
    @GetMapping("/bar2")
    public Result bar2() {
        Map<String, Object> map = new HashMap<>();
        List<String> xList = new ArrayList<>();
        List<Object> yList = new ArrayList<>();

        List<Singer> singers = singerService.selectTop(10);
        for (Singer singer : singers) {
            xList.add(singer.getName());
            yList.add(singer.getHot());
        }
        map.put("xList", xList);
        map.put("yList", yList);
        return Result.success(map);
    }

    /**
     * Top10 专辑统计
     */
    @GetMapping("/bar3")
    public Result bar3() {
        Map<String, Object> map = new HashMap<>();
        List<String> xList = new ArrayList<>();
        List<Object> yList = new ArrayList<>();

        List<Album> albums = albumService.selectTop(10);
        for (Album album : albums) {
            xList.add(album.getName());
            yList.add(album.getHot());
        }
        map.put("xList", xList);
        map.put("yList", yList);
        return Result.success(map);
    }
}
